package com.jayanslow.lighting.server;
import java.io.IOException;
import java.net.ServerSocket;

import com.jayanslow.lighting.server.clients.ClientManager;

public class Server extends Thread {
	public final int PORT = 2000;
	
	private ServerSocket serverSocket;
	private volatile boolean listening = true;
	private ClientManager cman;
	
    public Server(ClientManager cman) {
    	super("LightingServerThread");
    	this.cman = cman;
    	try {
    		serverSocket = new ServerSocket(PORT);
    	} catch (IOException e) {
    		System.err.println("Could not listen on port " + PORT);
    		return;
    	}
    }
    public void run() {
    	while (listening) {
    		try {
				cman.addClient(serverSocket.accept());
			} catch (IOException e) { }
    	}
    }
    public void end() {
    	listening = false;
    	try { serverSocket.close(); }
    	catch (IOException e) { }    	
    }
}